﻿using MediatR;
using Microsoft.EntityFrameworkCore;
using Traceability.Commont.Domains.AnimalsDomainFiles;
using Traceability.Commont.DTOs.CommontFiles;
using Traceability.Commont.Infrastructures.Interfaces;
using U.Enterprise.Api.Applications.Client;

namespace U.Enterprise.Api.Applications.Scalesetting
{
    public class LiveAnimalWeightDeleteHandler : IRequestHandler<LiveAnimalWeightDeleteCommand, Resqults>
    {
        private readonly IBateRepository<LiveAnimalweighing> _liveanimalweighingRepository;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="liveanimalweighingRepository"></param>
        public LiveAnimalWeightDeleteHandler(IBateRepository<LiveAnimalweighing> liveanimalweighingRepository)
        {
            _liveanimalweighingRepository = liveanimalweighingRepository;
        }

        /// <summary>
        /// 活畜称重逻辑删除
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<Resqults> Handle(LiveAnimalWeightDeleteCommand request, CancellationToken cancellationToken)
        {
            var resqults = new Resqults() { Code = 0 };
            try
            {
                var res = await _liveanimalweighingRepository.GetAll().Where(p => p.LiveAnimalweighingId == request.LiveAnimalweighingId).FirstAsync();
                if(res != null)
                {
                    res.State = 1;
                    var list = await _liveanimalweighingRepository.Update(res);
                    if (list > 0)
                    {
                        resqults.Date = list;
                        resqults.Messges = "删除成功";
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }
            return resqults;
        }
    }
}
